﻿using System;
using System.Collections.Generic;
using Domain.Infrastructure;

namespace Application.Infrastructure
{
    /// <summary>
    /// 登陆用户信息为空
    /// </summary>
    public class UserIdEmptyException : Exception
    {
        public override string Message => "数据校验错误,操作员Id为空";
    }

    /// <summary>
    ///  通用服务基类(效率不高)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseService<T>
        where T : class, new()
    {
        public BaseService()
        {
        }

        public BaseService(ILog log, IRepositoryBase<T> repository, string userId)
        {
            UserId = userId;
            Log = log;
            Repository = repository;
        }
        /// <summary>
        /// 登陆id
        /// </summary>
        public string UserId { get; set; }
        /// <summary>
        /// 日志
        /// </summary>
        public ILog Log;
        /// <summary>
        ///领域接口 
        /// </summary>
        public IRepositoryBase<T> Repository;
        /// <summary>
        /// 校验 仓库实体是否为空
        /// </summary>
        public void Check(T entity)
        {

            var model = entity as Entity<T>;
            if (model != null)
            {
                if (UserId == null)
                {
                    throw new UserIdEmptyException();
                    //if (model.OperatorUserId == null)
                    //{
                    //    model.OperatorUserId = UserId;
                    //}
                    //else
                    //    throw new UserIdEmptyException();
                }
                //  else if (model.OperatorUserId == null)


            }
        }

        ///  <summary>
        ///  添加
        ///  </summary> 
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Insert(T entity)
        {
            try
            {
                Check(entity);
                if (entity is ICreationAudited temp)
                {
                    temp.Create(UserId);
                }
                return Repository.Insert(entity) ;
            }
            catch (Exception ex)
            {

                Log.Error(ex);

                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Update(T entity)
        {
            try
            {
                Check(entity);
                if (entity is IModifyAudited temp)
                {
                    temp.Modify(UserId, temp.Id);
                }

                return Repository.Update(entity);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
        }

        /// <summary>
        ///删除 逻辑删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Delete(T entity)
        {
            try
            {
                if (entity is IDeleteAudited temp)
                {
                    temp.Remove(UserId);
                    return Repository.Update(entity);
                }
                else
                {
                    return Repository.Delete(entity);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Delete(string id)
        {
            try
            {
                return Repository.Delete(id);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
        }

        /// <summary>
        /// 得到实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetEntity(string id)
        {
            try
            {
                return Repository.GetEntity(id);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
        }

        /// <summary>
        /// 得到 全部list
        /// </summary>
        /// <returns></returns>
        public virtual List<T> GetList()
        {
            try
            {
                return Repository.GetList();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
        }
        /// <summary>
        /// 通过分页条件得到 全部list
        /// </summary>
        /// <param name="pagination"></param>
        /// <returns></returns>
        public virtual List<T> GetList(Pagination pagination)
        {
            try
            {
                return Repository.GetList(pagination);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
        }

    }
}
